package com.example.myhttplibrary.callback;

import com.example.myhttplibrary.exceptiopn.ExceptionEngine;
import com.google.gson.JsonElement;

public abstract class BaseCallBack<T> extends BaseObserver {
    boolean isConvert = true;

    @Override
    public void onNext(Object o) {
        T bean = parse(o.toString());
        if (isConvert && isSuccessFull()) {
            onSuccess(bean);
        }
    }

    T parse(String json) {
        T t = null;
        try {
            t = onConvert(json);
            isConvert = true;

        } catch (Exception e) {
            isConvert = false;
            error("解析异常", ExceptionEngine.ANALYTIC_CLIENT_DATA_ERROR);
        }
        return t;
    }

    //子类扩展解析
    protected abstract T onConvert(String result);

    //回调我们想要的数据
    protected abstract void onSuccess(T t);

    //解析我们具体的类
    public abstract T convert(JsonElement jsonElement);

    public abstract boolean isSuccessFull();
}
